package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Product;
import ebedynky.beans.ProductCategory;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * @author Ondrej David (davidon1)
 */
public class ProductCategoryService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public ProductCategoryService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds ProductCategory to the db.
     * @param cat ProductCategory to be inserted
     * @return ID of the added ProductCategory
     */
    public int addProductCategory(ProductCategory cat) {
        session.beginTransaction();
        int id = getMaxProductCategoryID() + 1;
        cat.setProductCategoryID(id);
        id = (Integer) session.save(cat);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of ProductCategories.
     * @return Max ID
     */
    public int getMaxProductCategoryID() {
        String hql = "select max(p.productCategoryID) from ProductCategory p";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxcatid = (Integer) list.get(0);
            return maxcatid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a ProductCategory identified by the ID.
     * @param id of the ProductCategory
     * @return ProductCategory with the ID given (null if not found)
     */
    public ProductCategory getProductCategoryByID(int id) {
        session.beginTransaction();
        ProductCategory cat = (ProductCategory) session.get(ProductCategory.class, new Integer(id));
        session.getTransaction().commit();
        return (cat);
    }

    /**
     * Retrieves a list of all ProductCategories stored in the db.
     * @return List of all ProductCategories (null if no ProductCategories)
     */
    public List<ProductCategory> getAllProductCategories() {
        session.beginTransaction();
        List<ProductCategory> catList = session.createQuery("from ProductCategory").list();
        session.getTransaction().commit();
        return (catList);
    }

    /**
     * Updates a specified ProductCategory identified by ID.
     * @param newCat an updated ProductCategory
     */
    public void updateProductCategory(ProductCategory newCat) {
        session.beginTransaction();
        ProductCategory cat = (ProductCategory) session.get(Product.class, new Integer(newCat.getProductCategoryID()));
        cat.setProductCategoryID(newCat.getProductCategoryID());
        cat.setCategory(newCat.getCategory());

        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the ProductCategory from db.
     * @param id ProductCategory ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteProductCategory(int id) {
        try {
            session.beginTransaction();
            ProductCategory cat = (ProductCategory) session.get(ProductCategory.class, new Integer(id));
            session.delete(cat);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }

    /**
     * Returns a list containing ProductCategories ordered by name.
     * @param name of the ProductCategory to be selected
     * @return List of ProductCategories, List containing ProductCategory with match, null if ProductCategory not found
     */
    public List<ProductCategory> getProductCategoryByName(String name) {
        session.beginTransaction();
        List<ProductCategory> cat = null;
        if (name.equals("") || name == null) {
            cat = session.createCriteria(ProductCategory.class).addOrder(Order.asc("category")).list();
        } else {
            cat = session.createCriteria(ProductCategory.class).add(Restrictions.like("category", "%" + name + "%")).addOrder(Order.asc("category")).list();
        }
        session.getTransaction().commit();
        return (cat);
    }
}
